#ifndef _DANIEL_KERR_UNIVERSEBUILDER_H
#define _DANIEL_KERR_UNIVERSEBUILDER_H

#include "driver.h"

struct gameOptions
{
	gameOptions() { file = ""; }
	bool randomGame;
	string file;
	int noPlay;
	int noPl;
	int noC;
};

void BuildUniverse(gameOptions &gopt)
{
	ofstream ofs("levels\\curGame.txt");
	
	if(gopt.randomGame)
	{
		// Story
		string stcmd = "copy levels\\story\\defaultStory.txt levels\\curStory.txt";
		system(stcmd.c_str());
		ofs<<"defaultStory.txt"<<endl<<endl;

		// Players
		ofs<<gopt.noPlay<<endl<<endl<<endl;
		
		// Planets
		int pID = 0;

		ofs<<gopt.noPl<<endl;
		
		for(int i=0;i<gopt.noPl;++i)
		{
			ofs<<(rand()/(RAND_MAX*2))+0.5<<"\t";
			
			if((pID < gopt.noPlay)&&(i==(pID*(int)(gopt.noPl/gopt.noPlay))))
			{ofs<<pID++<<"\t"<<rand()%11+10<<"\t"<<rand()%2+2;}
			else
			{ofs<<-1<<"\t"<<0<<"\t"<<rand()%5+1;}

			ofs<<"\tRANDOM";
			ofs<<"\tRANDOM";
			ofs<<endl;
		}
		ofs<<endl<<endl;

		// Edges
		for(int j=0;j<gopt.noPl;++j)
		{ ofs<<j<<"\t"<<(j+1)%gopt.noPl<<"\t"<<rand()%3+1<<endl; }

		int addEdge = ceil(((GLfloat)gopt.noC/10.0)*((((gopt.noPl-1)*gopt.noPl)/2)-gopt.noPl));

		vector <bool> temp;
		for(int e=0;e<gopt.noPl;++e)		{temp.push_back(0);}
		vector < vector <bool> > edges;
		for(int f=0;f<gopt.noPl;++f)		{edges.push_back(temp);}

		for(int k=0;k<addEdge;++k)
		{
			int start,end;
			do
			{
				start = rand()%gopt.noPl;
				do { end = rand()%gopt.noPl; }
				while((start==end)||((start+1)%gopt.noPl==end)||((start-1)%gopt.noPl==end));
			}while(edges[start][end]!=0);

			edges[start][end] = edges[end][start] = 1;
			
			ofs<<start<<"\t"<<end<<"\t"<<rand()%3+1<<endl;
		}

		ofs.close();	
	}
	else
	{
		string fname = "levels\\data\\" + gopt.file;
		ifstream ifs(fname.c_str());

		string storyfile;
		ifs >> storyfile;

		ifs.close();
		ofs.close();

		string stcmd = "copy levels\\story\\" + storyfile + " levels\\curStory.txt";
		system(stcmd.c_str());

		string cmd = "copy levels\\data\\" + gopt.file + " levels\\curGame.txt"; 
		system(cmd.c_str());
	}
}

#endif